package org.shopen.puremvc.view
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.TestingProxy;
	import org.shopen.puremvc.model.domain.testing.CheckTest;
	import org.shopen.puremvc.model.domain.testing.ChoiceTest;
	import org.shopen.puremvc.model.domain.testing.InputTest;
	import org.shopen.puremvc.model.domain.testing.Test;
	import org.shopen.puremvc.model.domain.testing.Testing;
	import org.shopen.puremvc.view.components.testing.CheckTestBox;
	import org.shopen.puremvc.view.components.testing.ChoiceTestBox;
	import org.shopen.puremvc.view.components.testing.InputTestBox;
	import org.shopen.puremvc.view.components.testing.TestBox;
	import org.shopen.puremvc.view.components.testing.TestBoxFactory;
	import org.shopen.puremvc.view.components.testing.TestingPanel;

// TODO:
	public class TestingPanelMediator extends Mediator
	{
		public static const NAME : String = "TestingPanelMediator";
		
		protected function get testingPanel() : TestingPanel
		{
			var result : TestingPanel = viewComponent as TestingPanel;
			return result;
		}
		
		[Bindable]
		protected var testing : Testing;
		
		public function TestingPanelMediator(viewComponent:Object=null)
		{
			super(TestingPanelMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			addListeners();
			retrieveTesting();
			initTests();
			testingPanel.setTestingState();
		}
		
		override public function getMediatorName():String
		{
			return TestingPanelMediator.NAME;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.TEST_RESULT,
					ApplicationConstants.START_TESTING];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var noteName : String = notification.getName();
			switch (noteName)
			{
				case ApplicationConstants.TEST_RESULT:
					var resultPoints : Number = notification.getBody() as Number;
					testingPanel.points += resultPoints;
					facade.removeMediator(notification.getType());
					break;
				case ApplicationConstants.START_TESTING:
					retrieveTesting();
					initTests();
					testingPanel.setTestingState();
					break;
			}
		}
		
		protected function initTests() : void
		{
			var tests : ArrayCollection = new ArrayCollection();
			var test : Test;
			for each (test in testing.tests)
			{				
				var testBox : TestBox;
				switch (test.getType())
				{
					case CheckTest.TYPE:
						testBox = TestBoxFactory.createTestBox(CheckTestBox.NAME);
						break;
					case ChoiceTest.TYPE:
						testBox = TestBoxFactory.createTestBox(ChoiceTestBox.NAME);
						break;
					case InputTest.TYPE:
						testBox = TestBoxFactory.createTestBox(InputTestBox.NAME);
						break;
					default:
						testBox = TestBoxFactory.createTestBox(TestBox.NAME);
						break;
				}
				tests.addItem(testBox);
				facade.registerMediator(new TestBoxMediator(testBox, test));
			}
			testingPanel.initTests(tests);
		}
		
		protected function retrieveTesting() : void
		{
			var testingProxy : TestingProxy = facade.retrieveProxy(TestingProxy.NAME) as TestingProxy;
			this.testing = testingProxy.getRandomTesting();
		}
		
		protected function addListeners() : void
		{
			if (testingPanel != null)
			{
				testingPanel.addEventListener(TestingPanel.NEWTESTING_EVENT, newTestingListener);
				testingPanel.addEventListener(TestingPanel.ACCOMPLISH_EVENT, accomplishListener);
			}
		}
		
		protected function newTestingListener(event : Event) : void
		{
			sendNotification(StateMachine.ACTION, null, ApplicationConstants.ACTION_START_TESTING);
		}
		
		protected function accomplishListener(event : Event) : void
		{
			testingPanel.setSummaryState(0, testing.maxPoints);
			sendNotification(ApplicationConstants.TESTOUT);
		}
	}
}